package com.lcw.lostandfound.service.Impl;

import com.lcw.lostandfound.mapper.CategoryMapper;
import com.lcw.lostandfound.pojo.Category;
import com.lcw.lostandfound.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findAllCategory() {
        return categoryMapper.findAllCategory();
    }

    @Override
    public int deleteById(Integer id) {
        return categoryMapper.deleteById(id);
    }

    @Override
    public int addCategory(Category category) {
        return categoryMapper.addCategory(category);
    }


    @Override
    public String convertCategory(String categoryIds) {
        if (categoryIds == null || "".equals(categoryIds.trim())) {
            return "";
        } else {
            if (!categoryIds.contains(",")) {
                return categoryMapper.queryCategoryName(categoryIds);
            }else {
                List<String> names = categoryMapper.queryCategoryNames(Arrays.asList(categoryIds.split(",")));
                StringBuilder sb = new StringBuilder(20);
                for (String name : names) {
                    sb.append(name).append("  ");
                }
                String ret = sb.toString();
                return ret.substring(0, ret.lastIndexOf("  "));
            }
        }
    }
}
